package com.booksystem.campuslibrarymanagementsystem.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class HashUtil {
    private HashUtil(){}

    //哈希密码加密
    private static String getSHA256Hash(String password) {
        try {
            // Get an instance of the SHA-256 digest
            MessageDigest md = MessageDigest.getInstance("SHA-256");

            // Convert the password string to bytes
            byte[] passwordBytes = password.getBytes(StandardCharsets.UTF_8);

            // Compute the hash
            byte[] hashedBytes = md.digest(passwordBytes);

            // Convert the byte array to a hexadecimal string
            StringBuilder sb = new StringBuilder();
            for (byte b : hashedBytes) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Error while hashing password", e);
        }
    }

    public static String HashGet(String psw){
       return getSHA256Hash(psw);
    }
}
